<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false" width="700px"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="污水池名称" prop="name">
        <el-input v-model="dataForm.name"  placeholder="污水池名称" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="液位计编号" prop="sensorId">
        <el-input v-model="dataForm.sensorId" placeholder="液位计名称" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="收运单位" prop="ctUnitId">
        <el-select v-model="dataForm.ctUnitId" placeholder="请选择" style="width: 480px;">
          <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="污水池容量" prop="containerCapacity">
        <el-input v-model="dataForm.containerCapacity" oninput="value=value.replace(/[^0-9.]/g,'')" placeholder="污水池容量" style="width: 480px;">
          <template slot="append">米</template>
        </el-input>
      </el-form-item>
      <el-form-item label="分配车辆次数" prop="initCalCount">
        <el-input v-model="dataForm.initCalCount" oninput="value=value.replace(/[^0-9]/g,'')" placeholder="预警后平台自动分配车辆次数，次数用完则会通知管理员手动分配车辆" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="污水池预警值" prop="containerWarningLiquidValue">
        <el-input v-model="dataForm.containerWarningLiquidValue" placeholder="污水池预警值" oninput="value=value.replace(/[^0-9.]/g,'')" style="width: 480px;">
          <template slot="append">米</template>
        </el-input>
      </el-form-item>
      <el-form-item label="地址" prop="containerAddress">
        <el-input v-model="dataForm.containerAddress" placeholder="请输入污水池地址" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="纬度" oninput="value=value.replace(/[^0-9.]/g,'')" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="lon">
        <el-input v-model="dataForm.lon" placeholder="经度" oninput="value=value.replace(/[^0-9.]/g,'')" style="width: 480px;"></el-input>
      </el-form-item>
    
      <el-form-item label="拉运去向" prop="containerDeliveryPoint">
        <el-input v-model="dataForm.containerDeliveryPoint" placeholder="拉运去向"></el-input>
      </el-form-item>
      <el-form-item label="街道" prop="jiedao">
        <el-input v-model="dataForm.jiedao" placeholder="请输入街道" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="村" prop="sheqv">
        <el-input v-model="dataForm.sheqv" placeholder="请输入村" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="污水池状态" prop="status">
        <el-radio-group v-model="dataForm.status" style="width: 480px;">
          <el-radio label="0">正常</el-radio>
          <el-radio label="1">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" placeholder="备注" maxlength="180" show-word-limit style="width: 480px;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    unitList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        sensorId: '',
        ctUnitId: '',
        containerCapacity: '',
        initCalCount: '',
        containerWarningLiquidValue: '',
        containerAddress: '',
        containerDeliveryPoint:'',
        lat: '',
        lon: '',
        name: '',
        containerType: '',
        jiedao: '',
        sheqv: '',
        status: '0',
        remark: ''
      },
      dataRule: {
        name: [
          { required: true, message: '污水池名称', trigger: 'blur' }
        ],
        sensorId: [
          { required: true, message: '液位计id不能为空', trigger: 'blur' }
        ],
        ctUnitId: [
          { required: true, message: '收运单位id不能为空', trigger: 'change' }
        ],
        containerCapacity: [
          { required: true, message: '污水池容量不能为空', trigger: 'blur' }
        ],
        initCalCount: [
          { required: true, message: '预警时自动分配车辆次数初始化值', trigger: 'blur' }
        ],
        containerWarningLiquidValue: [
          { required: true, message: '污水池预警值不能为空', trigger: 'blur' }
        ],
        containerAddress: [
          { required: false, message: '地址不能为空', trigger: 'blur' }
        ],
        lat: [
          { required: true, message: '纬度不能为空', trigger: 'blur' }
        ],
        lon: [
          { required: true, message: '经度不能为空', trigger: 'blur' }
        ],
        containerType: [
          { required: true, message: '污水池类型不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '污水池状态不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sign/signcontainer/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.sensorId = data.signContainer.sensorId
              this.dataForm.ctUnitId = data.signContainer.ctUnitId
              this.dataForm.containerCapacity = data.signContainer.containerCapacity
              this.dataForm.initCalCount = data.signContainer.initCalCount
              this.dataForm.containerWarningLiquidValue = data.signContainer.containerWarningLiquidValue
              this.dataForm.model = data.signContainer.model
              this.dataForm.containerAddress = data.signContainer.containerAddress
              this.dataForm.lat = data.signContainer.lat
              this.dataForm.lon = data.signContainer.lon
              this.dataForm.containerType = data.signContainer.containerType
              this.dataForm.containerColor = data.signContainer.containerColor
              this.dataForm.status = data.signContainer.status
              this.dataForm.remark = data.signContainer.remark
              this.dataForm.name = data.signContainer.name
              this.dataForm.jiedao = data.signContainer.jiedao
              this.dataForm.sheqv = data.signContainer.sheqv
              this.dataForm.containerDeliveryPoint = data.signContainer.containerDeliveryPoint
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sign/signcontainer/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'sensorId': this.dataForm.sensorId,
              'ctUnitId': this.dataForm.ctUnitId,
              'containerCapacity': this.dataForm.containerCapacity,
              'initCalCount': this.dataForm.initCalCount,
              'containerWarningLiquidValue': this.dataForm.containerWarningLiquidValue,
              'containerAddress': this.dataForm.containerAddress,
              'lat': this.dataForm.lat,
              'lon': this.dataForm.lon,
              'containerType': this.dataForm.containerType,
              'containerColor': this.dataForm.containerColor,
              'status': this.dataForm.status,
              'remark': this.dataForm.remark,
              'name': this.dataForm.name,
              'sheqv': this.dataForm.sheqv,
              'jiedao': this.dataForm.jiedao,
              'containerDeliveryPoint':this.dataForm.containerDeliveryPoint
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
